package com.lj.back.manage.scaffold.module.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.back.manage.scaffold.common.DTO.LoginFail;
import com.lj.back.manage.scaffold.common.DTO.LoginResult;
import com.lj.back.manage.scaffold.common.DTO.LoginSuccess;
import com.lj.back.manage.scaffold.common.DTO.ValidCodeDTO;
import com.lj.back.manage.scaffold.common.enums.ResponseEnum;
import com.lj.back.manage.scaffold.common.enums.UserStatusEnum;
import com.lj.back.manage.scaffold.common.util.Assert;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.module.kaptcha.KaptchaUtil;
import com.lj.back.manage.scaffold.module.sys.DO.SysUserTokenDO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysLoginDTO;
import com.lj.back.manage.scaffold.module.sys.DTO.SysUserDTO;
import com.lj.back.manage.scaffold.module.sys.constant.UserConstant;
import com.lj.back.manage.scaffold.module.sys.dao.SysUserTokenDao;
import com.lj.back.manage.scaffold.module.sys.service.SysLoginService;
import com.lj.back.manage.scaffold.module.sys.service.SysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 后台登录
 * @author lv
 * @version 1.0.0
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysLoginServiceJdbcImpl implements SysLoginService {

    private final SysUserService sysUserService;

    private final SysUserTokenDao sysUserTokenDao;

    private final KaptchaUtil kaptchaUtil;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public LoginResult login(SysLoginDTO loginDTO){
        ValidCodeDTO validCodeDTO = BeanUtil.copyProperties(loginDTO,ValidCodeDTO.class);
        ResponseEnum valid = kaptchaUtil.valid(validCodeDTO);
        if (valid != ResponseEnum.SUCCESS){
            return new LoginFail(valid);
        }
        SysUserDTO user = sysUserService.getUser(loginDTO.getLoginKey());
        if (user == null){
            return new LoginFail(ResponseEnum.LOGIN_FAIL);
        }
        if (Objects.equals(user.getStatus(), UserStatusEnum.LOCK.getCode())){
            return new LoginFail(ResponseEnum.LOGIN_USER_LOCK);
        }
        if (!BCrypt.checkpw(loginDTO.getPassword(), user.getPassword())){
            return new LoginFail(ResponseEnum.LOGIN_FAIL);
        }
        //删除旧token
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",user.getId());
        sysUserTokenDao.delete(queryWrapper);
        //生成token
        String token = CommonUtil.getUUID();
        //入库token
        SysUserTokenDO sysUserTokenDO = new SysUserTokenDO();
        sysUserTokenDO.setToken(token);
        sysUserTokenDO.setExpireTime(LocalDateTime.now().plusSeconds(UserConstant.USER_TOKEN_DEFAULT_EXPIRE_TIME));
        sysUserTokenDO.setUserId(user.getId());
        sysUserTokenDao.insert(sysUserTokenDO);
        return new LoginSuccess(token);
    }

    @Override
    public boolean logout(String token){
        Assert.assertNotNull(token, ResponseEnum.TOKEN_IS_NULL);
        QueryWrapper<SysUserTokenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("token",token);
        sysUserTokenDao.delete(queryWrapper);
        return true;
    }
}
